/**
 * Created by Administrator on 2016/6/27.
 */
"use strict";
swan_pc
    .controller("addClipController",function($scope,$state,ClipPost,$timeout,$rootScope,TimeExchange,TimeCheck,Task,ngDialog){
        $scope.tasks=[];
        $rootScope.PresentDialogContactIdNameString="";
        $rootScope.PresentDialogCompanyIdNameString="";
        $rootScope.PresentDialogOppIdNameString="";
        //$scope.resultCheck=null;
        Task.query(function(result) {
            $scope.tasks = result;
            /*angular.forEach($scope.tasks,function(data,index,array){
                console.log(index+":"+data.name);
            })*/
        })
        $scope.addclip=
        {
            "actionRemark": null,
            "address": null,
            "advanceInfo": null,
            "category": null,
            "checkList": null,
            "code": null,
            "color": null,
            "deletedAt": null,
            "endTime": null,
            "individualTaskId": null,
            "isDelayed": null,
            "isFixed": null,
            "isImportant": null,
            "isPrincipal": null,
            "isSecret": null,
            "level": null,
            "linkedActivityId": null,
            "linkedActivityType": null,
            "name": null,
            "organizationId": null,
            "ownerId": null,
            "previewTime": null,
            "relatedCompany": null,
            "relatedContact":null,
            "relatedOpp": null,
            "remark": null,
            "remind": null,
            "startTime":null,
            "status":null,
            "type": null,
            "who": null
        },
        $scope.status_show=function(option){
            if(option=="DRAFT"||option=="草稿") return "草稿";
            if(option=="CONFIRM"||option=="确认") return "确认";
            if(option=="CANCELED"||option=="取消") return "取消";
            if(option=="CLOSED"||option=="关闭") return "关闭";
        }
        $scope.judge_show=function(option){
            if(option=="1") return "是";
            if(option=="0") return "否";
        }
        $scope.init=function() {
            console.log("addClip State:"+$rootScope.toState.name);
            if($rootScope.toState.name=="companyaddcalendar"){
                var str=$scope.presentCompanyInfo.id+":"+$scope.presentCompanyInfo.companyBaseInfo.name;
                $scope.addclip.relatedCompany=str;
                //console.log("1::::",+$scope.addclip.relatedCompany);
            }
            if($rootScope.toState.name=="oppaddclip"){
                var str=$scope.presentOppInfo.id+":"+$scope.presentOppInfo.opportunity.oppName;
                $scope.addclip.relatedOpp=str;
            }
            $scope.level_array = [{"key": "主事项", "value": "主事项"}, {"key": "见缝插针", "value": "见缝插针"}];
            $scope.status_array = [{"key": "DRAFT", "value": "草稿"},
                {"key": "CONFIRM", "value": "确认"}, {"key": "CANCELED", "value": "取消"},
                {"key": "CLOSED", "value": "关闭"}, {"key": "PENDING", "value": "待定"}];
            $scope.title="新建日程"+$scope.addclip.name+"的详细信息";
        }
        $scope.init();
        $scope.ClipKey={
            "name":"日程内容",
            "startTime":"开始时间",
            "endTime":"结束时间",
            "address":"日程地点",
            "level":"日程类型",
            "actionRemark":"行动备注" ,
            "advanceInfo": "高级信息",
            "category" : "公事/私事",
            "checkList" : "物品检查" ,
            "code": "编号",
            "color" : "颜色" ,
            "createdBy":null,
            "createdDate": null,
            "deletedAt" : "删除时间" ,
            "id" : "ID" ,
            "individualTaskId": "任务ID" ,
            "isDelayed":"是否拖延",
            "isFixed": "是否固定时间",
            "isImportant": "是否重要" ,
            "isPrincipal" : "是否属于组织" ,
            "isSecret": "是否私密",
            "lastModifiedBy":null,
            "lastModifiedDate":null,
            "linkedActivityId": "link活动Id" ,
            "linkedActivityType":"link活动类型",
            "organizationId" : "组织ID" ,
            "ownerId" : "UserID" ,
            "previewTime" : "预计时间",
            "relatedCompany": "公司",
            "relatedContact":"相关联系人",
            "relatedOpp": "机会",
            "remark": "备注" ,
            "remind": "提醒时间" ,
            "status":"状态",
            "type": "类型" ,
            "who" : "行动对象"
        }
        $scope.init_show=function(){
            $scope.InfoShow=true;
        }
        $scope.init_show();
        $scope.display=function(option){
            if(option==0){
                $scope.InfoShow=false;
            }
            if(option==1){
                $scope.InfoShow=true;
            }
        }
        //console.log($rootScope.clickDate);
        var clickDate=$rootScope.clickedDate;
        //var clipStartTime = TimeExchange.GMTtoBjTime(clickDate);
        $scope.addclip.startTime=clickDate;
        $scope.addclip.endTime=clickDate;
        $scope.TimeShow=function(time){
            if(time==null){
                return "";
            }
            var timer=time.toString();
            timer=TimeExchange.StringtoLocalTime(timer);
            return timer;
        };
        $scope.show=function(index){
            var x=document.getElementById("show"+index);
            //alert(x.value);
            $timeout(function(){
                x.focus();
            });
        };
        $scope.clipStatusValueToKey=function(option){
            angular.forEach($scope.status_array,function(data,index,array){
                if(option==data.value){
                    $scope.addclip.status=data.key;
                }
            })
        }
        $scope.AddToDatabase=function(){
            if ($scope.addclip.name == null || $scope.addclip.name == "") {
                swal("日程内容不能为空", $scope.addclip.name, "error");
            }else {
                $scope.clipStatusValueToKey($scope.addclip.status);
                if ($scope.addclip.status == "DRAFT") {
                    ClipPost.save($scope.addclip,
                        function (result) {
                            $rootScope.calendarDirty = false;
                            $scope.$parent.loadClipList();
                            swal("添加成功", $scope.addclip.name, "success");
                            if ($rootScope.toState.name == "companyaddcalendar") {
                                $state.go("companycalendar");
                            } else if ($rootScope.toState.name == "oppaddclip") {
                                $state.go("oppclip");
                            } else {
                                $state.go("calendarshow");
                            }
                        },
                        function (result) {
                            swal("添加失败", $scope.addclip.name, "error");
                        }
                    )
                } else {
                    if ($scope.addclip.level != "主事项" && $scope.addclip.level != "见缝插针") {
                        swal("日程类型必须为主事项或见缝插针", $scope.addclip.name, "error");
                    } else {
                        //console.log("level"+$scope.addclip.level);
                        if ($scope.addclip.level == "主事项") {
                            var flag = TimeCheck.timecheck($scope.addclip.startTime, $scope.addclip.endTime);
                            //var flag=2;
                            if (flag == 0) {
                                swal("日程不能跨天", $scope.addclip.name, "error");
                            }
                            if (flag == 1) {
                                swal("日程不能短于30分钟", $scope.addclip.name, "error");
                            }
                            if (flag == 3) {
                                swal("结束时间不得早于开始时间", $scope.addclip.name, "error");
                            }
                            if (flag == 2) {
                                ClipPost.save($scope.addclip,
                                    function (result) {
                                        $scope.$parent.loadClipList();
                                        $rootScope.calendarDirty = false;
                                        swal("添加成功", $scope.addclip.name, "success");
                                        if ($rootScope.toState.name == "companyaddcalendar") {
                                            $state.go("companycalendar");
                                        } else if ($rootScope.toState.name == "oppaddclip") {
                                            $state.go("oppclip");
                                        } else {
                                            $state.go("calendarshow");
                                        }
                                    },
                                    function (result) {
                                        if(result.status=="409"){
                                            swal("添加失败", "您当前添加的日程与原有日程时间冲突，请修改后提交", "error");
                                        }else {
                                            swal("添加失败", $scope.addclip.name, "error");
                                        }
                                    }
                                )
                            }
                        } else {
                            ClipPost.save($scope.addclip,
                                function (result) {
                                    $scope.$parent.loadClipList();
                                    $rootScope.calendarDirty = false;
                                    swal("添加成功", $scope.addclip.name, "success");
                                    if ($rootScope.toState.name == "companyaddcalendar") {
                                        $state.go("companycalendar");
                                    } else if ($rootScope.toState.name == "oppaddclip") {
                                        $state.go("oppclip");
                                    } else {
                                        $state.go("calendarshow");
                                    }
                                },
                                function (result) {
                                    swal("添加失败", $scope.addclip.name, "error");
                                }
                            )
                        }
                    }
                }
            }
        }
        $scope.BackToCal=function(){
            $rootScope.goSref("calendarshow");
        };
        $scope.SelectForContact=function(){
            $rootScope.PresentDialogContactIdName={};
            //console.log("in:addclip.relatedContact::"+$scope.addclip.relatedContact);
            if ($scope.addclip.relatedContact != null && $scope.addclip.relatedContact != "") {
                //console.log("Contact FuZhi is run");
                var contact = $scope.addclip.relatedContact;
                contact = contact.split(",");
                for (var i = 0; i < contact.length; i++) {
                    var contactIdName = contact[i].split(":");
                    console.log("contactIdName"+contactIdName);
                    $rootScope.PresentDialogContactIdName[contactIdName[0]] = contactIdName[1];
                }
            }
            //console.log("in:PresentContactIdName:::::"+$scope.PresentContactIdName);
            ngDialog.open({
                templateUrl:"scripts/components/myself/calendar/dialog.html",
                scope:$scope,
                controller:["$scope",function($scope){
                    $scope.CloseContactDialog=function(){
                        $rootScope.PresentDialogContactIdNameString=$rootScope.PresentDialogContactIdName.toString();
                        ngDialog.closeAll();
                    }
                }]
            })
        }
        //console.log("addclip.relatedCompany2:"+$scope.addclip.relatedCompany)
        $scope.SelectForCompany=function(){
            $rootScope.PresentDialogCompanyIdName={};
            $scope.singleResult={};
            //console.log("in:addclip.relatedCompany::"+$scope.addclip.relatedCompany);
            if ($scope.addclip.relatedCompany != null && $scope.addclip.relatedCompany != "") {
                console.log("Company FuZhi is run");
                var company = $scope.addclip.relatedCompany;
                company = company.split(",");
                for (var i = 0; i < company.length; i++) {
                    var companyIdName = company[i].split(":");
                    console.log("companyIdName"+companyIdName);
                    $rootScope.PresentDialogCompanyIdName[companyIdName[0]] = companyIdName[1];
                }
            }
            //console.log("in:PresentContactIdName:::::"+$scope.PresentContactIdName);
            ngDialog.open({
                templateUrl:"scripts/components/myself/calendar/companydialog.html",
                scope:$scope,
                controller:["$scope",function($scope){
                    $scope.CloseCompanyDialog=function(){
                        $rootScope.PresentDialogCompanyIdNameString=$rootScope.PresentDialogCompanyIdName.toString();
                        ngDialog.closeAll();
                    }
                }]
            })
        }
        $scope.SelectForOpp=function(){
            $rootScope.PresentDialogOppIdName={};
            $scope.singleResult={};
            //console.log("in:addclip.relatedCompany::"+$scope.addclip.relatedCompany);
            if ($scope.addclip.relatedOpp != null && $scope.addclip.relatedOpp != "") {
                console.log("Opp FuZhi is run");
                var opp = $scope.addclip.relatedOpp;
                opp = opp.split(",");
                for (var i = 0; i < opp.length; i++) {
                    var oppIdName = opp[i].split(":");
                    console.log("OppIdName"+oppIdName);
                    $rootScope.PresentDialogOppIdName[oppIdName[0]] = oppIdName[1];
                }
            }
            //console.log("in:PresentContactIdName:::::"+$scope.PresentContactIdName);
            ngDialog.open({
                templateUrl:"scripts/components/myself/calendar/oppdialog.html",
                scope:$scope,
                controller:["$scope",function($scope){
                    $scope.CloseCompanyDialog=function(){
                        $rootScope.PresentDialogOppIdNameString=$rootScope.PresentDialogOppIdName.toString();
                        //console.log("OppResult:"+$rootScope.PresentDialogCompanyIdName);
                        //console.log("OppString:"+$rootScope.PresentDialogCompanyIdNameString);
                        ngDialog.closeAll();
                    }
                }]
            })
        }
        $scope.relatedCompanyInfoShow=function(){
            if($rootScope.PresentDialogCompanyIdNameString!="" &&$rootScope.PresentDialogCompanyIdNameString!=undefined){
                $scope.addclip.relatedCompany=$rootScope.PresentDialogCompanyIdNameString;
                //console.log("in:"+$scope.addclip.relatedContact);
                var CompanyShow=$scope.ContactCompanyInfoSplit($scope.addclip.relatedCompany);
                return CompanyShow;
            }else{
                //console.log("relatedCompany:"+$scope.addclip.relatedCompany);
                var CompanyShow=$scope.ContactCompanyInfoSplit($scope.addclip.relatedCompany);;
                return CompanyShow;
            }
        }
        $scope.relatedContactInfoShow=function(){
            if($rootScope.PresentDialogContactIdNameString!=""&&$rootScope.PresentDialogContactIdNameString!=undefined){
                $scope.addclip.relatedContact=$rootScope.PresentDialogContactIdNameString;
                //console.log("in:"+$scope.addclip.relatedContact);
                var ContactShow=$scope.ContactCompanyInfoSplit($scope.addclip.relatedContact);
                return ContactShow;
            }else{
                var ContactShow=$scope.ContactCompanyInfoSplit($scope.addclip.relatedContact);
                return ContactShow;
            }
        }
        $scope.relatedOppInfoShow=function(){
            if($rootScope.PresentDialogOppIdNameString!=""&&$rootScope.PresentDialogOppIdNameString!=undefined){
                $scope.addclip.relatedOpp=$rootScope.PresentDialogOppIdNameString;
                //console.log("in:"+$scope.addclip.relatedContact);
                var OppShow=$scope.ContactCompanyInfoSplit($scope.addclip.relatedOpp);
                return OppShow;
            }else{
                var OppShow=$scope.ContactCompanyInfoSplit($scope.addclip.relatedOpp);
                return OppShow;
            }
        }
        $scope.ContactCompanyInfoSplit=function(str){
            //console.log("str:"+str);
            if(str=="" || str==null){
                return str;
            }else{
                var str_res="";
                var str_new=str.split(",");
                for(var i=0;i<str_new.length;i++){
                    var strRes=str_new[i].split(":");
                    //console.log("strRes:"+strRes);
                    str_res=str_res+strRes[1]+","
                    //console.log("str_res"+str_res);;
                }
                var length=str_res.length-1;
                var resultString=str_res.substring(0,length);
                //console.log("resultString:"+resultString);
                return resultString;
            }
        }
        /*$scope.$watch($scope.addclip.relatedContact,function(newValue,oldValue){
            console.log("new:"+newValue+"  "+"old:"+oldValue);
        })*/
    });